package com.moonhill.bt.demo;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.moonhill.bt.BTConfig;

/**
 * @author Jagie
 * 
 *         MIDlet
 */
public class MoonHillBTMIDlet extends MIDlet implements CommandListener {
	static List list;
	BTConfig config;
	public static TextBox server_log = new TextBox("服务器", "", 1000,
			TextField.ANY);
	public static List remoteDevices = new List("找到以下服务器", List.IMPLICIT);
	public static TextBox client_log = new TextBox("client box", "", 1000,
			TextField.ANY);
	DemoClient client;
	DemoServer server;
	public static MoonHillBTMIDlet me;

	public MoonHillBTMIDlet() {
		me = this;
		list = new List("MoonHill", List.IMPLICIT);
		list.append("Client", null);
		list.append("Server", null);
		list.addCommand(new Command("退出",Command.EXIT,1));
		list.setCommandListener(this);
		
		remoteDevices.addCommand(new Command("退出", Command.EXIT, 2));
		remoteDevices.setCommandListener(this);

		client_log.addCommand(new Command("发送", Command.OK, 1));
		client_log.addCommand(new Command("退出", Command.EXIT, 2));
		client_log.setCommandListener(this);

		server_log.addCommand(new Command("发送", Command.OK, 1));
		server_log.addCommand(new Command("退出", Command.EXIT, 2));
		server_log.setCommandListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		config = new DemoConfig();
		Display.getDisplay(this).setCurrent(list);

	}

	/**
	 * debug����
	 * 
	 * @param s
	 *            Ҫ��ʾ���ִ�
	 */

	public void showString(String s) {
		Displayable dp = Display.getDisplay(this).getCurrent();
		Alert al = new Alert(null, s, null, AlertType.INFO);
		al.setTimeout(2000);
		Display.getDisplay(this).setCurrent(al, dp);
	}

	/**
	 * ��ʾ��˵�
	 * 
	 */

	public void showMainMenu() {
		Display.getDisplay(this).setCurrent(list);
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	public void commandAction(Command com, Displayable disp) {
		if (disp == list) {
			if (com == List.SELECT_COMMAND) {
				List list = (List) disp;
				int index = list.getSelectedIndex();
				if (index == 1) {
					server_log.setString("");
					server = new DemoServer(config);
					server.startCreateServer();
				} else {
					client_log.setString("");
					Display.getDisplay(this).setCurrent(remoteDevices);
					client = new DemoClient(config);
					client.startSearchGameServer();
				}

			} else {
				// exit
				me.notifyDestroyed();

			}
		} else if (disp == remoteDevices) {
			if (com.getCommandType() == Command.EXIT) {
				client.clearBt();
				remoteDevices.deleteAll();
				setDisplay(list, null);
			} else {
				int index = list.getSelectedIndex();
				client.connect2GameServer(index);
			}

		} else if (disp == server_log) {
			if (com.getCommandType() == Command.EXIT) {
				server.destroyServer();
				setDisplay(list, new Alert("", "服务器已销毁!", null, AlertType.INFO));
			} else if (com.getCommandType() == Command.OK) {
				server.sendMsg(DemoGame.getMsgBytes(DemoGame.PlayerID_SERVER,
						DemoGame.PlayerID_CLIENT, server_log.getString()));
			}
		} else if (disp == client_log) {
			if (com.getCommandType() == Command.EXIT) {
				client.disConnetion();
				setDisplay(remoteDevices, new Alert("", "退出服务器", null,
						AlertType.INFO));
			} else if (com.getCommandType() == Command.OK) {

				client.sendMsg(DemoGame.getMsgBytes(DemoGame.PlayerID_CLIENT,
						DemoGame.PlayerID_SERVER, client_log.getString()));

			}
		}
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	public static void setDisplay(Displayable disp, Alert alert) {
		if (alert == null) {
			Display.getDisplay(me).setCurrent(disp);
		} else {
			Display.getDisplay(me).setCurrent(alert, disp);
		}

	}

	public static void appendLog(boolean isServer, String s) {
		if (isServer) {
			server_log.setString(server_log.getString() + s + "\n");
		} else {
			client_log.setString(server_log.getString() + s + "\n");
		}
	}

}
